#include"head.h"

using namespace std;

int SplitTAG(char* word) {
	map<string, int>::iterator iter, liter;
	vector<element> vec;
	string contain(word);
	int len = contain.size();
	int Tbegin = 0;
	
	for (int i = 0; i < len; i++) {
		int cut = 1;
		iter = dic.find(contain.substr(i, cut));

		while (iter != dic.end()) {
			cut++;
			liter = iter;
			iter = dic.find(contain.substr(i, cut));

			if ((iter != dic.end()) && !strcmp(iter->first.c_str(), liter->first.c_str())) break;
		}

		if (cut == 1 && iter == dic.end()) continue;
		else {
			if (i - Tbegin>0) {
				string sub = contain.substr(Tbegin, i - Tbegin);
				vec.resize(vec.size() + 2);

				vec[vec.size() - 2].belong = T_TAG;
				strcpy(vec[vec.size() - 2].have, sub.c_str());
			}
			else vec.resize(vec.size() + 1);

			vec.back().belong = liter->second;
			strcpy(vec.back().have, (liter->first).c_str());

			i += cut - 2;
			Tbegin = i + 1;
		}
	}

	if (Tbegin == 0) return T_TAG;
	else {
		if (Tbegin < len) {
			vec.resize(vec.size() + 1);

			strcpy(vec.back().have, contain.substr(Tbegin, len - Tbegin).c_str());

			if (IsNumber(vec.back().have)) vec.back().belong = T_NUB;
			else vec.back().belong = T_TAG;

		}

		for (int i = 0; i < vec.size(); i++) {
			Container[WordOffset + i] = vec[i];
		}

		WordOffset += vec.size() - 1;
		return Container[WordOffset].belong;
	}

	return 0;
}

void IllegalChar(vector<element>& contain) {

	for (int i = 0; i < WordOffset; i++) {
		if (contain[i].belong == T_TAG) {
			for (int s = 0; s < strlen(contain[i].have); s++) {
				char temp = contain[i].have[s];
				if ((temp < 'a' || temp>'z')
					&& (temp < 'A' || temp>'Z')
					&& (temp < '0' || temp>'9')) {
					cout << "Illegal charactor at " << Container[i].have << " which is " << temp << '\n';
				}
			
			}
		}
	}
}